package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.MyAttentionBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.mine.ui.FansAndIdolActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Administrator on 2017/12/22 0022.
 */

public class FansAndIdolAdapter extends BaseRecyclerViewAdapter<MyAttentionBean.DataBean.ConcernBean> {

    private static final int MY_IDOL_TYPE = 0;
    private static final int MY_FANS_TYPE = 1;
    private static final int BOTTOM_COUNT_TYPE = 3;

    private Context mContext;
    private int mType;

    private OnCancelFollowClickListener mOnCancelFollowClickListener;
    private OnFollowClickListener mOnFollowClickListener;
    private OnItemClickListener mOnItemClickListener;

    public void setOnCancelFollowClickListener(OnCancelFollowClickListener onCancelFollowClickListener) {
        mOnCancelFollowClickListener = onCancelFollowClickListener;
    }

    public void setOnFollowClickListener(OnFollowClickListener onFollowClickListener) {
        mOnFollowClickListener = onFollowClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    public FansAndIdolAdapter(Context context, int type) {
        mContext = context;
        mType = type;
    }

    @Override
    public int getItemCount() {
        if (mList.isEmpty()) {
            return 0;
        } else {
            return mList.size() + 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mList.size()) {
            return BOTTOM_COUNT_TYPE;
        }
        switch (mType) {
            case MY_IDOL_TYPE:
                return MY_IDOL_TYPE;
            case MY_FANS_TYPE:
                return MY_FANS_TYPE;
        }
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case MY_IDOL_TYPE:
                return new IdolViewHolder(getConvertView(mContext, R.layout.item_my_idol, parent));
            case MY_FANS_TYPE:
                return new FansViewHolder(getConvertView(mContext, R.layout.item_my_fans, parent));
            case BOTTOM_COUNT_TYPE:
                return new BottomViewHolder(getConvertView(mContext, R.layout.item_fans_idol_bottom_count, parent));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyAttentionBean.DataBean.ConcernBean bean = null;
        if (position < mList.size()) {
            bean = mList.get(position);
        }
        if (holder instanceof IdolViewHolder) {
            ((IdolViewHolder) holder).setData(bean);
            ((IdolViewHolder) holder).setListener(bean);
        } else if (holder instanceof FansViewHolder) {
            ((FansViewHolder) holder).setData(bean);
            ((FansViewHolder) holder).setListener(bean);
        } else if (holder instanceof BottomViewHolder) {
            ((BottomViewHolder) holder).setData(mList.size());
        }
    }

    /**
     * 我关注的人 view holder
     */
    class IdolViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_user_avatar)
        QMUIRadiusImageView mUserAvatar;
        @BindView(R.id.tv_user_name)
        TextView mUsername;
        @BindView(R.id.tv_user_university)
        TextView mUserUniversity;
        @BindView(R.id.btn_cancel_follow)
        TextView mBtnCancelFollow;

        IdolViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void setData(MyAttentionBean.DataBean.ConcernBean bean) {
            // 如果不是当前登录用户的粉丝关注列表则隐藏取消关注按钮
            if (!TextUtils.equals(UserInfoBean.getUid(mContext), bean.getFromUid())) {
                mBtnCancelFollow.setVisibility(View.GONE);
            }

            // 用户头像
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(350, 350);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(bean.getPhoto()) ? R.drawable.default_avatar : HttpRequestConstant.USER_PIC_HEAD + bean.getPhoto())
                    .apply(requestOptions)
                    .into(mUserAvatar);

            // 用户昵称
            mUsername.setText(bean.getName());

            // 大学
            mUserUniversity.setText(bean.getUnit());
        }

        void setListener(final MyAttentionBean.DataBean.ConcernBean bean) {
            mBtnCancelFollow.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnCancelFollowClickListener != null) {
                        mOnCancelFollowClickListener.onCancelFollow(bean, getAdapterPosition());
                    }
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null){
                        mOnItemClickListener.onItemClick(bean, getAdapterPosition());
                    }
                }
            });
        }
    }

    /**
     * 关注我的人 view holder
     */
    class FansViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_user_avatar)
        QMUIRadiusImageView mUserAvatar;
        @BindView(R.id.tv_user_name)
        TextView mUsername;
        @BindView(R.id.tv_user_university)
        TextView mUserUniversity;
        @BindView(R.id.btn_follow)
        TextView mBtnFollow;

        FansViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void setData(MyAttentionBean.DataBean.ConcernBean bean) {
            // 如果不是当前登录用户的粉丝关注列表则隐藏取消关注按钮
            if (!TextUtils.equals(UserInfoBean.getUid(mContext), bean.getToUid())) {
                mBtnFollow.setVisibility(View.GONE);
            }

            // 用户头像
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(350, 350);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(bean.getPhoto()) ? R.drawable.default_avatar : HttpRequestConstant.USER_PIC_HEAD + bean.getPhoto())
                    .apply(requestOptions)
                    .into(mUserAvatar);

            // 用户昵称
            mUsername.setText(bean.getName());

            // 大学
            mUserUniversity.setText(bean.getUnit());
        }

        void setListener(final MyAttentionBean.DataBean.ConcernBean bean) {
            switch (bean.getM()) {
                case 0:
                    mBtnFollow.setText("关注");
                    mBtnFollow.setOnClickListener(new BGAOnNoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            if (mOnFollowClickListener != null) {
                                mOnFollowClickListener.onFollow(bean, getAdapterPosition());
                            }
                        }
                    });
                    break;
                case 1:
                    mBtnFollow.setText("取消关注");
                    mBtnFollow.setOnClickListener(new BGAOnNoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            if (mOnCancelFollowClickListener != null) {
                                mOnCancelFollowClickListener.onCancelFollow(bean, getAdapterPosition());
                            }
                        }
                    });
                    break;
            }

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null){
                        mOnItemClickListener.onItemClick(bean, getAdapterPosition());
                    }
                }
            });
        }
    }

    class BottomViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_bottom_count)
        TextView mCount;

        BottomViewHolder(View convertView) {
            super(convertView);
            ButterKnife.bind(this, convertView);
        }

        public void setData(int count) {
            mCount.setText(String.format(mContext.getResources().getString(R.string.zone_fans_count), count));
        }
    }

    public interface OnCancelFollowClickListener {
        void onCancelFollow(MyAttentionBean.DataBean.ConcernBean bean, int position);
    }

    public interface OnFollowClickListener {
        void onFollow(MyAttentionBean.DataBean.ConcernBean bean, int position);
    }

    public interface OnItemClickListener{
        void onItemClick(MyAttentionBean.DataBean.ConcernBean bean, int position);
    }
}
